import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gameplay_flutter/lib_baseview/utils/constant/app_colors.dart';
import 'package:gameplay_flutter/lib_runtime/extension/string_extension/string_text.dart';
import 'package:gameplay_flutter/lib_runtime/extension/widget/widget_wrapper.dart';

import '../lib_baseview/lib_baseview.dart';
import '../lib_baseview/theme/jr.dart';
import '../lib_baseview/widget/web/common_web_view.dart';
import '../lib_network/old/url_config.dart';
import '../lib_runtime/global_config.dart';
import '../lib_runtime/web_url_const.dart';
import '../m_main/debug/debug_page.dart';
import '../resource/language/generated/l10n.dart';
import 'os_login_controller.dart';
import 'phone_login/login_with_phone_page.dart';

class OSLoginPage extends StatefulWidget {
  const OSLoginPage({Key? key}) : super(key: key);

  @override
  State<OSLoginPage> createState() => _OSLoginPageState();
}

class _OSLoginPageState extends State<OSLoginPage> {
  final OSLoginController ctrl = Get.put(OSLoginController());

  @override
  void dispose() {
    Get.delete<OSLoginController>(force: true);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<OSLoginController>(builder: (controller) {
      return Stack(
        children: [
          image(
            'login/login_bg.png',
            width: ScreenUtil().screenWidth,
            height: ScreenUtil().screenHeight,
            fit: BoxFit.fill,
          ),
          Scaffold(
            backgroundColor: Colors.transparent,
            body: Builder(builder: (context) {
              if (controller.loadingPlatforms) {
                return const Center(child: CupertinoActivityIndicator());
              }
              return SizedBox(
                width: ScreenUtil().screenWidth,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    // const Spacer(),
                    SizedBox(height: 85.h),
                    T.current.login_welcome1.text(
                        color: AppColors.COLOR_FFFFFF,
                        fontSize: 25,
                        weight: TextFontWeight.bold),
                    T.current.login_welcome2.text(
                        color: AppColors.COLOR_FFFFFF,
                        fontSize: 36,
                        weight: TextFontWeight.bold),

                    if (GlobalConfig.isDebug)
                      Container(
                        color: Colors.red,
                        child: InkWell(
                          child: Text(T.current.debug_page),
                          onTap: () {
                            DebugPage.show(context);
                          },
                        ),
                      ),

                    const Spacer(),

                    SizedBox(height: 17.h),

                    loginButtonWidget(
                      'login/login_phone_apple.png',
                      T.current.login_with_apple_ios,
                    ),
                    SizedBox(height: 17.h),

                    loginButtonWidget(
                      'login/login_phone_google.png',
                      T.current.login_with_google_ios,
                    ),
                    SizedBox(height: 17.h),

                    loginButtonWidget(
                      'login/login_phone_number.png',
                      T.current.login_with_phone_ios,
                    ).onTap(() {
                      /// 跳转 ---->
                      return Get.to(() => const LoginWithPhonePage());
                    }),

                    SizedBox(height: 25.h),

                    GetBuilder<OSLoginController>(
                      id: 'protocolSelect',
                      builder: (context) {
                        return _protocolWidget().padding(start: 50, end: 50);
                      },
                    ),
                    SizedBox(height: 70.h),
                  ],
                ),
              );
            }),
          ),
        ],
      );
    });
  }

  Widget loginButtonWidget(String loginIcon, String loginTitle,
      [String rightIcon = 'login/login_right_icon.png']) {
    return Container(
      height: 40.h,
      color: AppColors.COLOR_FFFFFF,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          SizedBox(width: 16.w),
          JR.img(loginIcon, width: 26.w, height: 26.w),
          const Spacer(),
          loginTitle.text(
              color: AppColors.COLOR_1B1B1B,
              fontSize: 14.sp,
              weight: TextFontWeight.medium),
          const Spacer(),
          JR.img(rightIcon, width: 18.w, height: 18.w),
          SizedBox(width: 16.w),
        ],
      ).padding(top: 7.h, bottom: 7.h),
    ).corner(radius: 20.h).padding(start: 50.w, end: 50.w);
  }

  Widget _protocolWidget() {
    TextStyle defaultStyle = TextStyle(
        color: const Color(0xFFFFFFFF).withAlpha((0.35 * 255).toInt()),
        fontSize: 10.0,
        fontWeight: FontWeight.w500);
    TextStyle linkStyle = const TextStyle(
        color: Color(0xFFFFFFFF), fontSize: 10, fontWeight: FontWeight.w500);

    return RichText(
      textAlign: TextAlign.center,
      text: TextSpan(style: defaultStyle, children: [
        TextSpan(
          text: T.current.login_registering_tips2,
        ),
        TextSpan(
          text: ' ${T.current.login_registering_user_agreement}',
          style: linkStyle,
          recognizer: TapGestureRecognizer()
            ..onTap = () async {
              String url =
                  '${UrlConfig.pageDomain}${WebUrlConst.registerAgreement.path}';
              CommonWebView.show(context, url);
            },
        ),
        TextSpan(
          text: ' ${T.current.loing_and} ',
        ),
        TextSpan(
          text: T.current.login_registering_privacy_policy,
          style: linkStyle,
          recognizer: TapGestureRecognizer()
            ..onTap = () async {
              String url =
                  '${UrlConfig.pageDomain}${WebUrlConst.privateAgreement.path}';

              CommonWebView.show(context, url);
            },
        ),
      ]),
    );
  }

  List<Widget> mainLoginPlatforms(OSLoginController controller) {
    List<Widget> children = [];
    // for (IThirdLoginPlatform platform in controller.mainLoginPlatforms) {
    //   children.add(FutureBuilder<bool>(
    //       future: platform.supportLogin(),
    //       builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
    //         if (snapshot.connectionState == ConnectionState.done &&
    //             snapshot.data == true) {
    //           return Padding(
    //             padding: const EdgeInsets.only(bottom: 12.0),
    //             child: platform.getLoginButton(context, onTap: () {
    //               controller.thirdLoginAction(platform);
    //             }),
    //           );
    //         }
    //         return const SizedBox.shrink();
    //       }));
    // }
    return children;
  }

  List<Widget> subLoginPlatforms(OSLoginController controller) {
    List<Widget> children = [];
    // for (IThirdLoginPlatform platform in controller.subLoginPlatforms) {
    //   children.add(FutureBuilder<bool>(
    //       future: platform.supportLogin(),
    //       builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
    //         if (snapshot.connectionState == ConnectionState.done &&
    //             snapshot.data == true) {
    //           return GestureDetector(
    //             onTap: () => controller.thirdLoginAction(platform),
    //             child: platform.getIcon(),
    //           );
    //         }
    //         return const SizedBox.shrink();
    //       }));
    // }
    return children;
  }
}
